<?php
/**
 * PC 端 友情链接公共控制器
 * linkscontroller.php
 * 
 * Create on 2015-4-10 19:25:48
 * Create by Jim.Chou
 */
namespace haibao\cms\business\control\pc;
use \haibao\cms\model\filter\link as LinkFilter;
use \haibao\cms\business\Link as LinkBusiness;
use \haibao\cms\business\ImageLibrary as ImageLibraryBusiness;
use haibao\frame\data\query\Condition;


class LinksController extends \haibao\cms\business\control\Base{
	
	const STYLE_STATIC_URL = 'http://static.localhaibao.cn:81/static/html/';
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}	
	/**
	 * 获取模版内容
	 * @param : String $content
	 */
	public function displayTemplateHtml(){
		/*
		 * type=1 海报站
		 * type=2 美剧站
		 * */
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$path = $this->generateHtmlPath(__CLASS__);
		$linkType = $controlData['manual']?$controlData['manual']['type']:1;
		$linkFilter = new LinkFilter();
		$linkBusiness = new LinkBusiness();
		
		$linkFilter->select(array(
				LinkFilter::CONDITION_FIELD_NAME_LINK_NAME,
				LinkFilter::CONDITION_FIELD_NAME_URL,
				LinkFilter::CONDITION_FIELD_NAME_LINK_TYPE,
				LinkFilter::CONDITION_FIELD_NAME_IMAGE_ID
			)
		);
		$linkFilter->where(LinkFilter::CONDITION_FIELD_NAME_IS_SHOW_WEB,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Link::IS_SHOW_WEB_YES);
		$linkFilter->where(LinkFilter::CONDITION_FIELD_NAME_SITE_TYPE,Condition::CONDITION_EQUAL,$linkType);
		$linkFilter->where(LinkFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Link::DEFAULT_STATUS_OK);
		$linkFilter->order(LinkFilter::CONDITION_FIELD_NAME_ORDER_NUM,false);
		$links =$linkBusiness->getLinkList($linkFilter);
		
		$datas = array();
		foreach($links as $key=>$list){
			if($list->LinkType == 1){
				$datas["imagetype"][$key]["imageurl"] =  $this->getUrlById($list->ImageId);
				$datas["imagetype"][$key]["url"] = $list->Url;
			}else{
				$datas["texttype"][$key]["linkname"] = $list->LinkName;
				$datas["texttype"][$key]["url"] = $list->Url;
			}
		}
		
		$this->assign('staticUrl',self::STYLE_STATIC_URL);
		$this->assign('datas',$datas);
		$this->generateHtml($path,$content);
		return $this->getHtml($path);
	}
	
	private function getUrlById($id){
		$imageLibraryBusiness = new ImageLibraryBusiness();
		$imageUrl = $imageLibraryBusiness->getImageUrlById($id);
		if($imageUrl){
			return $this->replaceImageLink($imageUrl[$id],150,50,100,0);
		}
	}
}